var app = getApp();
import commonAPI from '../../../api/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    repeatRecordsArr: [],
    tmprows:[],
    page: 1,
    appHeight: 0,
    loaded: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.getappsysteminfo(function (res) { // 设置app屏幕的高度
      that.setData({
        appHeight: res.windowHeight
      });
    });
    wx.hideShareMenu(); // 禁止分享
  },

  initData: function (freshType) {
    var that = this;
    wx.hideShareMenu(); // 禁止分享
    commonAPI.getCorrectResultList({ "pageNum": that.data.page,
    pageSize: 15 }).then(res => {
      if (res.code == '200') {
        var resArr = that.data.tmprows;
        wx.hideNavigationBarLoading(); // 完成停止加载图标
        wx.stopPullDownRefresh(); // 停止下拉
        if (res.data.list.length == 0) {
          wx.showToast({
            title: '没有更多数据了',
            icon: 'none',
            duration: 2000
          })
        } else {        
          if (freshType == "1") {
            resArr = res.data.list; // 下拉刷新
          } else if (freshType == "2") {
            resArr.push.apply(resArr, res.data.list); //上拉加载
          }
          const newWordInfoArr = that.dataTool(resArr);
          that.setData({
            repeatRecordsArr: newWordInfoArr,
            tmprows:resArr,
            loaded: true
          })
        }
      }
    })
  },

  dataTool:function(temArr){
    var dateArr = new Array();
    // 获取日期到数组
    for (var i in temArr) {
      dateArr.push(temArr[i].time);
    }
    // 去重复
    var newdateArr = Array.from(new Set(dateArr));
    var newWordInfoArr = new Array();
    for (var i in newdateArr) {
      var obj = {};
      obj.time = newdateArr[i];
      obj.wordInfoArr = [];
      newWordInfoArr.push(obj);
    }

    // 数据分装以时间为纬度
    for (var i in newWordInfoArr) {
      var wordObj = {};
      for (var j in temArr) {
        if (newWordInfoArr[i].time == temArr[j].time) {
          temArr[j].voiceResultInfo = temArr[j].time;
          newWordInfoArr[i].wordInfoArr.push(temArr[j]);
        }
      }
    }
    return newWordInfoArr;
  },

  // 再次跟读
  teacherCorrect: function (e) {
    var vrid = e.currentTarget.dataset.vrid;
    wx.navigateTo({
      url: '../../repeat/repeat?vrid=' + vrid
    })
  },

  // 去练习
  repeat2Index: function () {
    wx.switchTab({
      url: '../../index/index'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page:1
    })
    this.initData("1");
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    this.initData("1");
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var i = this.data.page;
    this.setData({
      page: ++i
    })
    this.initData("2");
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})